#define _CRT_SECURE_NO_WARNINGS 1
#pragma warning(disable:4996)//#pragma warning(disable:4996) 是 Visual Studio 特有的编译指示，用于禁用特定的编译器警告。
//#pragma warning：编译警告控制指令
//disable：禁用警告
//4996：警告编号，表示"使用了过时或不安全的函数"
#include<iostream>
#include<WinSock2.h>
#include<string>
#pragma comment(lib, "ws2_32.lib")//#pragma comment(lib, "ws2_32.lib") 是 Windows 平台特有的编译指令，用于链接指定的库文件。

using namespace std;

#define PORT 8080
int main()
{
	WSAData wsd;
	//启动Winsock
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0)
	{
		cout << "WSAStartup Error =" << WSAGetLastError() << endl;
		return 0;
	}
	else
	{
		cout << "WSAStartup Success" << endl;
	}
	SOCKET sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd == -1)//创建失败
	{
		cout << "socket error" << endl;
		exit(1);
	}
	else
	{
		cout << "socket Success" << endl;
	}
	const string ip ="49.233.107.211";
	//客户端不要需要显示的bind
	struct sockaddr_in server;
	memset(&server, 0, sizeof(server));
	server.sin_family = AF_INET;
	server.sin_addr.S_un.S_addr = inet_addr(ip.c_str());
	server.sin_port = htons(PORT);
#define NUM 1024
	char inbuffer[NUM];
	string buffer;
	while (true)
	{
		cout << "请输入#";
		getline(cin, buffer);
		int n = sendto(sockfd, buffer.c_str(), buffer.size(), 0, (struct sockaddr*)&server, sizeof(server));
		if (n < 0)
		{
			cout << "sendto error" << endl;
			break;
		}
		

		inbuffer[0] = 0;
		struct sockaddr_in peer;
		int peerlen = sizeof(peer);
		
		n = recvfrom(sockfd, inbuffer, sizeof(inbuffer) - 1, 0, (struct sockaddr*)&peer, &peerlen);
		if (n > 0)
		{
			inbuffer[n] = 0;
			cout << inbuffer << endl;
		}
		else break;
	}

	WSACleanup();
	closesocket(sockfd);

	return 0;

}

